package com.ueboot.weishop.service.setting.impl;

import com.ueboot.weishop.entity.setting.BrandEntity;
import com.ueboot.core.repository.BaseRepository;
import com.ueboot.weishop.repository.setting.BrandRepository;
import com.ueboot.core.service.impl.BaseServiceImpl;
import com.ueboot.weishop.service.setting.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@Service
public class BrandServiceImpl extends BaseServiceImpl<BrandEntity> implements BrandService {

    @Autowired
    private BrandRepository repository;

    /**
     * BrandEntity brandEntity = new BrandEntity();
     * brandEntity.setName("name");
     * ExampleMatcher matcher = ExampleMatcher.matching()
     * .withIgnoreNullValues()
     * .withMatcher("name", endsWith())
     * .withMatcher("lastname", startsWith().ignoreCase());
     * repository.findAll((root, query, cb) -> {
     * Predicate p1 = cb.like(root.get("name").as(String.class), "%" + brandEntity.getName() + "%");
     * Predicate p2 = cb.equal(root.get("uuid").as(Integer.class), um.getUuid());
     * Predicate p3 = cb.gt(root.get("age").as(Integer.class), um.getAge());
     * //把Predicate应用到CriteriaQuery中去,因为还可以给CriteriaQuery添加其他的功能，比如排序、分组啥的
     * query.where(cb.and(p3, cb.or(p1, p2)));
     * //添加排序的功能
     * query.orderBy(cb.desc(root.get("uuid").as(Integer.class)));)
     * return query.getRestriction();
     * });
     * return repository.findAll(Example.of(brandEntity, matcher), pageable);
     *
     * @param pageable
     * @return
     */

    /**
     * 根据关键字查询
     *
     * @param pageable
     * @param keyword
     * @return
     */
    @Override
    public Page<BrandEntity> findBy(Pageable pageable, String keyword) {
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName(keyword);
        brandEntity.setRemark(keyword);
        ExampleMatcher example = ExampleMatcher.matching()
                .withIgnoreNullValues()
                .withMatcher("name", matcher -> matcher.contains())
                .withMatcher("remark", matcher -> matcher.contains());
        return repository.findAll(Example.of(brandEntity, example), pageable);
    }

    @Override
    public List<BrandEntity> findByCategoryId(Long categoryId) {
        return repository.findByCategoryId(categoryId);
    }

    @Override
    protected BaseRepository getBaseRepository() {
        return repository;
    }

}
